using namespace std;
#include "commun.h"

int AFFICHAGE = 1;

char* dtd_fileName;

int xml_parse(XMLArbre**);
int dtd_parse(DTD**);

extern int xml_debug;
extern int dtd_debug;

extern FILE * xml_in;
extern FILE * dtd_in;

int xmlParser(char* filename, XMLArbre ** arbre)
{
	int err;
	FILE * fid;
	fid = fopen(filename, "r");
	xml_in = fid;
	err = xml_parse(arbre);
	if (err != 0) 
	{
		printf("Parse xml ended with %d error(s)\n", err);
		return 1;
	}

  	printf("Parse xml ended with success\n");
	fclose(fid);
	printf("**************************************************************************************************\n");
	
	if(AFFICHAGE)
	{
		(*arbre)->afficher();

		printf("**************************************************************************************************\n");
		printf("**************************************************************************************************\n");
	}
	return err;
}

int dtdParser(char* filename, DTD ** arbre)
{
	int err;
	FILE * fid;
	fid = fopen(filename, "r");
	dtd_in = fid;
	err = dtd_parse(arbre);
	if (err != 0) 
	{
		printf("Parse dtd ended with %d error(s)\n", err);
		return 1;
	}

  	printf("Parse dtd ended with success\n");
	fclose(fid);
	printf("**************************************************************************************************\n");
	
	if(AFFICHAGE)
	{
		(*arbre)->afficher();

		printf("**************************************************************************************************\n");
		printf("**************************************************************************************************\n");
	}
	return err;
}

int main(int argc, char **argv)
{
	int err;
	XMLArbre * arbreXML;
	XMLArbre * arbreXSL;
	DTD * arbreDTD;

	char* xml_file;
	char* xsl_file;

	//xml only
	if(strcmp(argv[1],"x") == 0)
	{
		xml_file = argv[2];
		printf("parsing xml file: %s :\n", xml_file);
		xmlParser(xml_file, &arbreXML);
	}
	
	//dtd only
	if(strcmp(argv[1],"d") == 0)
	{
		dtd_fileName = argv[2];
		printf("parsing dtd file: %s :\n", dtd_fileName);
		dtdParser(dtd_fileName, &arbreDTD);
	}

	//xml+dtd
	if(strcmp(argv[1],"xd") == 0)
	{
		xml_file = argv[2];
		printf("parsing xml file: %s :\n", xml_file);
		xmlParser(xml_file, &arbreXML);

		printf("parsing dtd file: %s :\n", dtd_fileName);
		dtdParser(dtd_fileName, &arbreDTD);
	}

	//verification
	if(strcmp(argv[1],"v") == 0)
	{
		xml_file = argv[2];
		printf("parsing xml file: %s :\n", xml_file);
		xmlParser(xml_file, &arbreXML);

		printf("parsing dtd file: %s :\n", dtd_fileName);
		dtdParser(dtd_fileName, &arbreDTD);

		VefifyConditions(arbreXML,arbreDTD);
	}

	//transformation
	if(strcmp(argv[1],"t") == 0)
	{
		xml_file = argv[2];
		xsl_file = argv[3];
		
		printf("parsing xml file: %s :\n", xml_file);
		xmlParser(xml_file, &arbreXML);

		printf("parsing xsl file: %s : \n", argv[2]);
		xmlParser(xsl_file, &arbreXSL);

		HTMLArbre * html = new HTMLArbre(arbreXSL, arbreXML);

		printf("Affichage du HTML\n");	
		html->afficher();
	}

	return 0;
}
